public class MySingleList {
public class ListNode {
    ListNode next;
    int val;

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode() {
    }
}

    private ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }


    private ListNode findIndex(int index) {
        ListNode cur = head;
        while(index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }


    //删除所有值为key的结点
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }

        //1.解决头为key的情况
        while(head.val==key) {
            head=head.next;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while(cur != null) {
            if(cur.val == key ) {
                prev.next = cur.next;
                cur = cur.next;
            }
            else {
                prev = cur;
                cur = cur.next;
            }
        }
        //2.解决头为key的情况
//        if(head.val == key) {
//            head=head.next;
//        }
    }


    public void remove (int key) {
        if(head == null) {
            return;
        }

        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode prev = searchPrev(key);
        if(prev == null) {
            System.out.println("没有这个数据");
            return;
        }
        ListNode del = prev.next;
        prev.next = del.next;
    }




    public ListNode searchPrev (int key) {
        ListNode prev = head;
        while(prev.next != null) {
            if(prev.next.val == key) {
                //delete
                return prev;
            }
            else {
                prev = prev.next;
            }
        }
        return null;
    }


    //头插
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //任意插入
    public void addInsert(int data, int index) {
        int len = size();
        if(index < 0 || index > len) {
            //抛异常
            System.out.println("index不合法");
        }

        if(index == 0) {
            addFirst(data);
            return;
        }

        ListNode cur = findIndex(index);

        ListNode node = new ListNode(data);

        node.next = cur.next;
        cur.next = node;
    }


    //尾插
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null) {
            head = node;
            return;
        }
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }



    //找key是否存在
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            //如果这里的val是引用类型，这里要用equals来进行比较
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //链表中结点的个数
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //遍历单链表
    public void show() {
        //这里不是定义的一个结点，这是一个引用变量
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void show(ListNode curPoint) {
        //这里不是定义的一个结点，这是一个引用变量
        ListNode cur = curPoint;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
       //方法1:
        while(head != null) {
            ListNode headNext = head.next;
            head.next = null;
            head = headNext;
        }
        //方法2:
        //this.head = null;
    }

    public ListNode reverseList() {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }


    //找链表中的倒数第k个结点
    public ListNode FindKthToTail(int k) {
        if(k<=0||k>size()) {
            return null;
        }

        ListNode fast = head;
        ListNode slow = head;
        while(k>0) {
            if(fast == null) {
                return null;
            }
            fast = fast.next;
            k--;
        }
        while(fast!=null) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
}
